jQuery(function($){
    $.use("space_list.waterfall",function(){
        var waterfall = $.wanjuhe.waterfall({"fullScreen":0}),
            wjh_waterfall = waterfall.wjh_waterfall,
            key=1,page= 1,more=$('.more-wp a.more');
        $('a.more').click(function(){
            if(key){
                key=0;
                more.html("加载中...");
                $.ajax({
                        "url": "ajax/list.php",
                        "type": "GET",
                        "dataType" : 'json',
                        "success":function(data){
                            if(data.state === "sucess"){
                                page++;
                                var JsonDataCache=[];
                                if(page % 4 == 0){$(".wjh-list").html('');wjh_waterfall.clearBefore();$(window).scrollTop(0);};
                                $(data.items).each(function(index,value){
                                    var v = $(value),contenter = $('#J_waterfall-contenter .wjh-list');
                                    if(contenter.length == 0){
                                        contenter = $('#J_waterfall-contenter');
                                    }
                                    v.css({"top":wjh_waterfall.cssHeight[0]+'px',"left":wjh_waterfall.cssWidth[0]+'px'}).appendTo(contenter);
                                    JsonDataCache.push(v);
                                });
                                wjh_waterfall.adjustPosition(JsonDataCache);
                                key=1;
                                more.html('显示更多<b class="ico-db-arrow"></b>');
                            }
                        },
                        "timeout" : 20000,
                        "error " : function(){
                        }
                    }
                );
            };
            return false;
        });
    });
    $.use("attention",function(){
        var attention_list = $('.attention-list2'),at_item = attention_list.find('.at-item');
        function removeAttention(el,type){
            var elem,hid,item;
            if(type==='at_item'){
                elem = $(el);
                hid = elem.attr('data-hid');
                item = $('.wjh-list .item[data-hid='+hid+']');
            };
            if(type==='item'){
                item = $(el);
                hid = item.attr('data-hid');
                elem = $('.attention-list2 .at-item[data-hid='+hid+']');
            };
            if(elem.data('init')){
                $('<li class="at-item"></li>').appendTo(elem.parent()).data('init',1);
            }
            elem.remove();
            item.removeClass('wjh-item-select');
            item.removeData('attention');
        };
        at_item.each(function(index,val){
            $(val).data('init',1);
        });
        function at_itemFix(text,hid){
            var elem;
            at_item.each(function(index,val){
               if(!$.trim($(val).html())){
                   $(val).html(text+'<a href="#" class="dele" title="删除"><span class="ico-dele">删除</span></a>').attr('data-nname',text).attr('data-hid',hid);
                   elem = $(val);
                   return false;
               }
            });
            return elem ? elem :false;
        };
        attention_list.delegate(".dele","click",function(){
            removeAttention($(this).parents('.at-item'),'at_item');
            return false;
        });
        $("#J_waterfall-contenter").delegate(".item","click",function(){
            var self=$(this),
                val=self.attr('data-nname'),
                hid = self.attr('data-hid'),
                el;
            if(self.find("span.add").length==0){
                $('<span class="add"><b class="ico-tick">添加</b></span>').appendTo(self);
            };
            if(self.data('attention')){
                removeAttention(self,'item');
            }else{
                if(el =at_itemFix(val,hid)){
                    el.addClass('att');
                }else{
                    $('<li class="att at-item" data-nname="'+val+'" data-hid="'+hid+'">'+val+'<a href="#" class="dele" title="删除"><span class="ico-dele">删除</span></a></li>').appendTo(attention_list);
                };
                self.data('attention',1);
                self.addClass('wjh-item-select');
            };
            return false;
        })
    })
});